package com.cema.manage.config;

import com.cema.manage.config.Interceptor.TokenInterceptor;
import com.cema.manage.project.manage.antiCheat.service.IAntiCheatService;
import com.cema.manage.project.manage.antiCheatDetail.service.IAntiCheatDetailService;
import com.cema.manage.redis.RedisService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ls on 2018/6/27.
 */
@Configuration
public class ViewConfigure extends WebMvcConfigurerAdapter {
    @Resource(name = "thymeleafViewResolver")
    private ThymeleafViewResolver thymeleafViewResolver;
    @Value("${qiniu.ym}")
    private String ym;
    @Value("${baidu.url}")
    private String baiduUrl;


    @Resource
    private RedisService redisService;

    @Resource
    private IAntiCheatDetailService iAntiCheatDetailService;

    @Resource
    private IAntiCheatService iAntiCheatService;

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        if (thymeleafViewResolver != null) {
            Map<String, Object> args = new HashMap<>(8);
            args.put("qniuUrl", ym);
            args.put("baiduUrl", baiduUrl);
            thymeleafViewResolver.setStaticVariables(args);
        }
        super.configureViewResolvers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        registry.addInterceptor(new TokenInterceptor(redisService, iAntiCheatDetailService, iAntiCheatService))
                // .addPathPatterns("/**") //拦截所有请求/upload/ecology
                .addPathPatterns("/user/info", "/user_reward/*", "/user/user_feedback", "/user/count_friend", "/user/user_friend"
                        , "/version_update/*", "/question_bank/*", "/withdrawal/*", "/user/afb_info", "/user/to_day_statistics", "/user/update_nickname"
                        , "/user/data_center", "/user/the_charts", "/user/separation","/user/logout", "/secondary/*","/pro_user/*")
                .excludePathPatterns("/user/registerOrLogin", "/page", "/ad_callback/*", "/config/*", "/app/*","/pro/*");
    }
}
